package com.xjve;

import com.ruoyi.framework.aspectj.lang.annotation.Anonymous;
import com.xjve.framework.hibernate.query.core.SqlQueryService;
import com.xjve.pojo.*;
import com.xjve.repository.SchoolRepository;
import com.xjve.repository.StudentRepository;
import com.xjve.repository.SysUserRepository;
import com.xjve.service.TestService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/jpaTest")
@Anonymous()
@RequiredArgsConstructor
public class JpaTestController {

    private final TestService testService;

    private final SysUserRepository sysUserRepository;

    private final SchoolRepository schoolRepository;

    private final SqlQueryService sqlQueryService;

    @GetMapping("/test")
    public String  test(){
//        Student byId = studentRepository.getById(2323L);
//        System.out.println(byId);


        Student student = new Student();
        student.setAge(22);
        student.setName("xjve");
        student.save();

        List<Student> xj = new StudentSearch().setName_fuzzy("xj").setAge(22).find();

        xj.forEach(x -> {
            x.setName(x.getName()+"hel");
            x.save();
        });


        List<SysUser> sysUsers = new SysUserSearch().setNickName("若依").find();

        sysUsers.forEach(s -> {
            s.setNickName("若依"+1);
            s.save();
        });


        //测试级联查询
        School school = new School();
        school.setName("林子校");
        school.save();

        student.setSchool(school);
        student.save();

        List<Student> students = new StudentSearch().setSchoolName_fuzzy("林子").find();
        System.out.println("学生数量:"+students.size());



        // 应该改为
        Map<String, Object> sqlParam = new HashMap<>();
        sqlParam.put("id",23232L);
        List<SysUser> query = sqlQueryService.query("user-sql-test",sqlParam , SysUser.class);


        return "student size:"+students.size()+",query-size:"+query.size();
    }


}
